﻿using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using NDK.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_PPG测量 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_PPG测量 Instance = new Station_PPG测量();
        public Stopwatch SunWay14 = new Stopwatch();
        public string StationName = "Station14PPG测量";



        protected int ppgGrrCount;
        public bool IsStartOnce;
        private bool[] ArrayHoleIsHave;
        public ConcurrentQueue<bool[]> QueueHoleIsHave;
        public ConcurrentQueue<MeasureProduct> PPGVaueQueue;

        //步骤字段、属性
        private StationStep _Step = StationStep.轴运动至取料位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if(_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            ppgGrrCount = 1;
            IsStartOnce = false;
            logBaseInfo = "PPG测量流程";
            Step = StationStep.轴运动至取料位;
            QueueHoleIsHave = new ConcurrentQueue<bool[]>();
            PPGVaueQueue = new ConcurrentQueue<MeasureProduct>();
            SunWay14.Start ();
        }

        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.轴运动至取料位:
                        if(MotionControl.AxesMove(Machine.pos_接料位_PPG搬运工位) && MotionControl.AxesMove(Machine.pos_安全位_PPG下压工位))
                        {
                            SignalRequest = true;
                            Step = StationStep.等待上工位放料完成;
                        }
                        break;

                    case StationStep.等待上工位放料完成:
                        if(Station_下料搬运.Instance.SignalComplete || Common.PPG循环启动)
                        {
                            SignalRequest = false;
                            Step = StationStep.轴运动至PPG下方位;
                        }
                        break;

                    case StationStep.轴运动至PPG下方位:
                        if(MotionControl.AxesMove(Machine.pos_PPG下方位_PPG搬运工位))
                        {
                            Step = StationStep.轴运动至PPG下压位;
                        }
                        break;

                    case StationStep.轴运动至PPG下压位:
                        if(MotionControl.AxesMove(Machine.pos_下压位_PPG下压工位))
                        {
                            if(!Common.GRR循环模式)
                            {
                                QueueHoleIsHave.TryDequeue(out ArrayHoleIsHave);
                                Station_下料翻转.Instance.QueueHoleIsHave.Enqueue(new bool[2] { ArrayHoleIsHave[0], ArrayHoleIsHave[1] });
                            }
                            else
                            {
                                ArrayHoleIsHave= new bool[2] {true,true};
                            }
                            Thread.Sleep(Common.IP_PPG保压时间);
                            Step = StationStep.PPG开始测量;
                        }
                        break;

                    case StationStep.PPG开始测量:
                        if(ArrayHoleIsHave[0])
                        {
                            var value = Math.Abs(Machine.PPG.Read("02") + Common.DP_PPG补偿2);
                            if(!Common.GRR循环模式)
                            {
                                PPGVaueQueue.Enqueue(new MeasureProduct() { PPGHole = 1, MeasureData = new List<DataItem>() { new DataItem() { Name = Common.SP_PPGFAI, Value = value } } });
                            }
                            else
                            {
                                var time = DateTime.Now;
                                string CSVDataDirectory = $"{Common.dataSavePath}\\{Common.ProjectName}\\{time:yyyy-MM}\\{Common.CurProType} Data\\{time:yyyy-MM-dd}\\PPG1.csv";
                                FileUtils.AppendText(CSVDataDirectory, value.ToString() + "\r\n");
                            }
                        }
                        if(ArrayHoleIsHave[1])
                        {
                            var value = Math.Abs(Machine.PPG.Read("01") + Common.DP_PPG补偿1);
                            if(!Common.GRR循环模式)
                            {
                                PPGVaueQueue.Enqueue(new MeasureProduct() { PPGHole = 2, MeasureData = new List<DataItem>() { new DataItem() { Name = Common.SP_PPGFAI, Value = value } } });
                            }
                            else
                            {
                                var time = DateTime.Now;
                                string CSVDataDirectory = $"{Common.dataSavePath}\\{Common.ProjectName}\\{time:yyyy-MM}\\{Common.CurProType} Data\\{time:yyyy-MM-dd}\\PPG2.csv";
                                FileUtils.AppendText(CSVDataDirectory, value.ToString() + "\r\n");
                            }
                        }
                        Step = StationStep.轴运动至安全位;
                        break;


                    case StationStep.轴运动至安全位:
                        if (MotionControl.AxesMove(Machine.pos_安全位_PPG下压工位))
                        {
                            if (!Common.GRR循环模式)
                            {
                                Step = StationStep.等待下工位要料;
                            }
                            else
                            {
                                ppgGrrCount++;
                                if (ppgGrrCount > Common.PPG循环次数)
                                {
                                    Common.PPG循环启动 = false;
                                    ppgGrrCount = 1;
                                }
                                Step = StationStep.轴运动至取料位;
                            }
                        }
                        break;

                    case StationStep.等待下工位要料:
                        if (Station_下料翻转.Instance.SignalRequest)
                        {
                            Step = StationStep.轴运动至接料位;
                        }
                        break;

                    case StationStep.轴运动至接料位:
                        if (MotionControl.AxesMove(Machine.pos_取料位_PPG搬运工位))
                        {
                            if (IsStartOnce)
                            {
                                IsStartOnce = false;
                                MachineStatusManager.CurrentMachineStatus = MachineStatus.Pause;
                                Step = StationStep.轴运动至取料位;
                            }
                            else
                            {
                                SignalComplete = true;
                                Step = StationStep.等待翻转工位取料完成;
                            }
                        }
                        break;

                    case StationStep.等待翻转工位取料完成:
                        if (!Station_下料翻转.Instance.SignalRequest)
                        {
                            //给PLC结果-TOM

                            SignalComplete = false;
                            Step = StationStep.轴运动至取料位;
                        }
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            轴运动至取料位,
            等待上工位放料完成,
            轴运动至PPG下方位,
            轴运动至PPG下压位,
            PPG开始测量,
            轴运动至安全位,
            等待下工位要料,
            轴运动至接料位,
            等待翻转工位取料完成
        }
    }

}
